#  -*- coding: utf-8 -*-

from .base_stop_loss import BaseStopLoss
from data.data_module import DataModule
from factor.factor_module import FactorModule
from util.stock_util import get_diff_dates



class KeepDatesStopLoss(BaseStopLoss):
    def __init__(self, account, max_keep_dates):
        BaseStopLoss.__init__(self, account)
        self.max_keep_dates = max_keep_dates

    def update_holding(self, code, date):
        pass

    def is_stop(self, code, date):
        """
        判断股票在当前日期是否需要止损
        :param code: 股票代码
        :param date: 日期
        :return: True - 止损， False - 不止损
        """
        print('止损判断：%s' % code, flush=True)
        buy_date = self.account.holding[code]["buy_date"]
        profit = self.account.holding[code]["profit"]
        keep_dates = get_diff_dates(code,buy_date,date)
        if profit < 5 and keep_dates > self.max_keep_dates:
            return True

        return False

